package com.ttai.presenter.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.Gson;

import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;

import com.ttai.model.net.Phone;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.MainActivity;
import com.ttai.ui.activity.UserHomePage;

import java.util.HashMap;


import retrofit2.Call;

public class HomePresrenter extends BasePresenter {
    MainActivity mainActivity;
    DBHelper dbhlper;
    public static String myPhone = "";
    private HashMap<String,String> errorMap;
    private String token;

    public HomePresrenter(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    protected void parseJson(JsonObject data) {
        Gson gson = new Gson();
//        Phone phone = gson.fromJson(data, Phone.class);
//        myPhone = phone.getPhone();
        Toast.makeText(mainActivity,"自动登录成功",Toast.LENGTH_SHORT).show();
        Intent intent2=new Intent();
        intent2.setClass(mainActivity,UserHomePage.class);
        mainActivity.startActivity(intent2);
    }



    @Override
    protected void showError(String message) {
        Toast.makeText(mainActivity,message,Toast.LENGTH_SHORT).show();
        mainActivity.finish();
        Intent intent2=new Intent();
        intent2.setClass(mainActivity,EnterPage.class);
        mainActivity.startActivity(intent2);


    }
    public void getTokenData(String token){
        Call<ResponseInfo> tokenInfo = responseInfoApi.tokenLogin(token);
        tokenInfo.enqueue(new CallBackAdapter());
    }

    public String getBeanToken() {
        token = "";
        dbhlper = DBHelper.getInstance(mainActivity);
        SQLiteDatabase db = dbhlper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from tokenacount",null);
        while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("id"));
            token =cursor.getString(cursor.getColumnIndex("token"));
        }
        cursor.close();
        return token;
    }
}
